package com.andaily.web.controller.web.cooking;

import com.andaily.domain.dto.cooking.CookingSettlementDto;
import com.andaily.domain.dto.cooking.CookingSettlementHistoryDto;
import com.andaily.domain.dto.cooking.SettlementFormDto;
import com.andaily.service.SettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/cooking/settlement")
public class SettlementController {

    @Autowired
    private SettlementService settlementService;

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public String showForm(Model model) throws Exception {
        SettlementFormDto settlementFormDto = settlementService.loadSettlementForm();
        model.addAttribute("settlementFormDto", settlementFormDto);
        return "cooking/settlement/settlement_form";
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String submit(@ModelAttribute("settlementFormDto") SettlementFormDto settlementFormDto) throws Exception {
        String guid = settlementService.submitSettlement(settlementFormDto);
        return "redirect:details/" + guid;
    }

    @RequestMapping("details/{guid}")
    public String details(@PathVariable("guid") String guid, boolean back, Model model) throws Exception {
        CookingSettlementDto cookingSettlementDto = settlementService.loadCookingSettlement(guid);
        model.addAttribute("cookingSettlementDto", cookingSettlementDto);
        model.addAttribute("back", back);
        return "cooking/settlement/settlement_details";
    }

    @RequestMapping("real_time")
    public String realTime(Model model) throws Exception {
        CookingSettlementDto cookingSettlementDto = settlementService.loadRealTimeCookingSettlement();
        model.addAttribute("cookingSettlementDto", cookingSettlementDto);
        return "cooking/settlement/real_time_settlement";
    }

    @RequestMapping("history")
    public String history(CookingSettlementHistoryDto overviewDto, Model model) throws Exception {
        overviewDto = settlementService.loadCookingSettlementHistory(overviewDto);
        model.addAttribute("overviewDto", overviewDto);
        return "cooking/settlement/settlement_history";
    }

}
